package com.emmmya.ocs.modules.system.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.emmmya.ocs.common.base.BaseDao;
import com.emmmya.ocs.modules.system.dao.UserDao;
import com.emmmya.ocs.modules.system.dao.mapper.UserMapper;
import com.emmmya.ocs.modules.system.entity.Role;
import com.emmmya.ocs.modules.system.entity.User;
import com.emmmya.ocs.modules.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
  @description:
  @author: 咕噜肉IT小店-黑医
  @createDate: 2020-05-12
  @version: 1.0
 */

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;

    @Autowired
    UserMapper userMapper;

    @Override
    public User findByUsername(String name) {
        return  userDao.findByUsername(name);
    }


    @Override
    public User findByUsernameP(String username) {
        return userDao.findByUsernameP(username);
    }

    @Override
    public Integer updateUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public User getUserById(Long id){
        return userMapper.selectById(id);
    }

    @Override
    public Integer updateEnhance(User user){
        //防止直接update的时候清空了Role，因为role使用的懒加载，再没使用的时候，role这个值为null，jpa会自动清空h_user_role表
        List<Role> roles = user.getRoles();
        if(!CollectionUtils.isEmpty(roles)) {
            Role role = roles.get(0);
            System.out.println(role.getRoleName());
        }
       return userMapper.updateById(user);
    }

    @Override
    public BaseDao<User, Long> getRepository() {
        return userDao;
    }

    @Override
    public Page<User> findAllByVoAndLimit(User user,Pageable pageable) {
        Specification<User> spec = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate and = criteriaBuilder.and();
                  if(!StringUtils.isEmpty(user.getUsername())){
                      Predicate username = criteriaBuilder.equal(root.get("username").as(String.class), user.getUsername());
                      and = criteriaBuilder.and(and,username);
                  }
                  if(user.getStatus()!=null) {
                      Predicate state = criteriaBuilder.equal(root.get("status").as(String.class), user.getStatus());
                      and = criteriaBuilder.and(and,state);
                  }
                  if(!StringUtils.isEmpty(user.getName())) {
                      Predicate name = criteriaBuilder.like(root.get("name").as(String.class), "%"+user.getName()+"%");
                      and = criteriaBuilder.and(and,name);
                  }
                 if(!StringUtils.isEmpty(user.getEmail())) {
                      Predicate email = criteriaBuilder.equal(root.get("email").as(String.class), user.getEmail());
                      and = criteriaBuilder.and(and,email);
                  }
                return and;
            }
        };
        return getRepository().findAll(spec, pageable);
    }

    @Override
    public void getListByPage(IPage<User> page, Wrapper<User> wrapper) {
        userMapper.selectPage(page,wrapper);
    }

    @Override
    public void disableUser(Long id) {

    }

}
